//
//  SetUpViewController.m
//  B-72K
//
//  Created by 张春洋 on 16/3/19.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "SetUpViewController.h"
#import "AllPrefixHeader.pch"
#import "SkinViewController.h"


#import "Scan_VC.h"
#import "ClearData.h"
@interface SetUpViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableVIew;
@property (nonatomic, strong)NSArray *dataArr;
@end

@implementation SetUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = NO;
    self.title = @"设置";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view NightWithType:UIViewColorTypeNormal];
    [self dataHandler];
}
- (void)dataHandler
{
    self.tableVIew = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    _tableVIew.delegate = self;
    _tableVIew.dataSource = self;
    [self.view addSubview:_tableVIew];
    [_tableVIew NightWithType:UIViewColorTypeNormal];
    _tableVIew.rowHeight = 60;
    [_tableVIew registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.dataArr =@[@"清理缓存", @"夜间模式", @"一键换肤", @"二维码"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell NightWithType:UIViewColorTypeNormal];
    cell.textLabel.text = _dataArr[indexPath.row];
    [cell.textLabel NightWithType:UIViewColorTypeNormal];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        CGFloat h = [ClearData folderSizeAtPath:path];
        
        
        UILabel *clearLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
        
        clearLabel.text = [NSString stringWithFormat:@"%.2fMB",h];
//        cell.textLabel.text = @"清除缓存";
        cell.accessoryView = clearLabel;
        [clearLabel sizeToFit];

        
    }
    if (indexPath.row == 1) {
        UISwitch *swith = [[UISwitch alloc] initWithFrame:self.view.bounds];
        cell.accessoryView = swith;
        [swith addTarget:self action:@selector(yejian:) forControlEvents:UIControlEventTouchUpInside];
        // 定义OFF状态下的颜色
//        swith.tintColor = [UIColor whiteColor];
        // 定义ON状态下的颜色
        swith.onTintColor = [UIColor colorWithRed:185/255.0 green:181/255.0 blue:154/255.0 alpha:1];
        // 定义圆形按钮的颜色
        swith.thumbTintColor = [UIColor colorWithRed:183/ 255.0 green:93/255.0 blue:94/255.0 alpha:1];

        NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
        swith.on = [userdefaults boolForKey:@"swith"];
    }
    return cell;
}
- (void)yejian:(UISwitch *)swith
{
    [ThemeManage shareThemeManage].isNight = ![ThemeManage shareThemeManage].isNight;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:nil];
    [[NSUserDefaults standardUserDefaults] setBool:[ThemeManage shareThemeManage].isNight forKey:@"night"];
    NSUserDefaults *userdefauls = [NSUserDefaults standardUserDefaults];
    [userdefauls setBool:swith.on forKey:@"swith"];
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_tableVIew deselectRowAtIndexPath:indexPath animated:NO];
    
    if (indexPath.row == 0) {
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:@"清除缓存后可能导致下次程序的缓慢" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *quxiao = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *queding = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            
            NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
            [ClearData clearCache:path];
          
            [self.tableVIew reloadData];
        }];
        [aler addAction:quxiao];
        [aler addAction:queding];
        [self presentViewController:aler animated:YES completion:^{
            
            
        }];

    }
    
    if (indexPath.row == 2) {
        SkinViewController *skin = [[SkinViewController alloc] init];
        [self.navigationController pushViewController:skin animated:YES];
        
    }if (indexPath.row == 3) {
        Scan_VC *scan = [[Scan_VC alloc] init];
        scan.modalTransitionStyle = UIModalTransitionStylePartialCurl;
        [self presentViewController:scan animated:YES completion:^{
            
            
        }];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
